/**
 * Copyright (c) 2010 by Kevin Bierhoff.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *    1. Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *    3. Neither the names of the authors nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.googlecode.syper.javasyp.formulae;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.eclipse.jdt.core.dom.ITypeBinding;

/**
 * @author kevin
 * @since Jun 6, 2010
 */
public class Fun implements Ref {

	/** Predicate used to signify {@code final} fields */
	public static final String FINAL_PRED = "final";
	/**
	 * Two uses:
	 * <ul>
	 * <li>no suffix: fields with protecting field permissions
	 *     (e.g., {@code protect f})
	 * <li>heap incarnation suffix: objects protecting other objects
	 *     (e.g., {@code protect0 this (derefObj0 this f)})
	 * </ul>
	 */
	public static final String PROTECT_PRED = "protect";

	public static Fun pred(String id, List<Formula> args) {
		return new Fun(id, args, null);
	}

	public static Fun pred(String id, Formula... args) {
		return new Fun(id, Arrays.asList(args), null);
	}

	private final String id;
	private final List<Formula> args;
	private final ITypeBinding type;

	Fun(String id, List<Formula> args, ITypeBinding type) {
		super();
		this.id = id;
		this.args = Collections.unmodifiableList(args);
		this.type = type;
	}

	public String getId() {
		return id;
	}

	public List<Formula> getArgs() {
		return args;
	}

	public ITypeBinding getType() {
		return type;
	}

	@Override
	public <T> T dispatch(FormulaVisitor<T> visitor) {
		return visitor.visit(this);
	}

	@Override
	public boolean isBoolean() {
		return type == null || TypingUtil.isBoolean(type);
	}

	@Override
	public String toString() {
		if (args.isEmpty())
			return id;
		else
			return "(" + id + " " + args + ")";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((args == null) ? 0 : args.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Fun other = (Fun) obj;
		if (args == null) {
			if (other.args != null)
				return false;
		} else if (!args.equals(other.args))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
}
